<?php
declare(strict_types=1);

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Logic\Account as AccountLogic;
use App\Logic\Role as RoleLogic;
use App\Http\Requests\Account as AccountRequest;
use Illuminate\Support\Facades\Validator;

class AccountController extends Controller
{
    private $accountLogic;
    private $roleLogic;
    private $accountRequest;

    public function __construct()
    {
        $this->accountLogic = new AccountLogic();
        $this->roleLogic = new RoleLogic();
        $this->accountRequest = new AccountRequest();
    }

    public function index()
    {
        $role = $this->roleLogic->all();
        return view('account.index', ['role' => $role]);
    }

    public function list(Request $request)
    {
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $start = ($page-1)*$limit;
        $where['account_name'] = $request->input('name','');
        $where['account'] = $request->input('account','');
        $where['role_id'] = $request->input('rid',0);
        $result = [
            'code' => 0,
            'msg' => '获取数据成功',
            'count' => $this->accountLogic->count($where),
            'data' =>  $this->accountLogic->list((int)$start, (int)$limit, $where),
        ];
        return response()->json($result);
    }

    public function add(Request $request)
    {
        if ($request->isMethod('POST')) {
            $result = ['status'=>false, 'msg'=>'新增失败！'];

            $data['role_id']= $request->post('role_id',0);
            $data['account_name']= $request->post('account_name','');
            $data['account_email'] = $request->post('account_email','');
            $data['account_phone'] = $request->post('account_phone','');
            $data['account'] = $request->post('account','');
            $data['password'] = $request->post('password','');
            $data['password_confirm'] = $request->post('password_confirm','');
            $data['account_status']= $request->post('account_status',0);

            $validator = Validator::make($request->all(), $this->accountRequest->rules(), $this->accountRequest->messages());
            if ($validator->fails()) {
                $result['msg'] = $validator->errors()->first();
                return response()->json($result);
            }

            if(!$this->accountLogic->checkAccount($data['account'])){
                $result['msg'] = '账号已经被占用！';
                return response()->json($result);
            }

            $data['password'] = md5('fengadmin&' . $data['password']);
            if ($this->accountLogic->add($data)){
                $result['status'] = true;
                $result['msg'] = '新增成功！';
                return response()->json($result);
            }

            return response()->json($result);

        } else {
            $role = $this->roleLogic->all();
            return view('account.add', ['role' => $role]);
        }
    }

    public function edit(Request $request, int $id = 0)
    {
        if ($request->isMethod('POST')) {
            $result = ['status'=>false, 'msg'=>'修改失败！'];

            $id= $request->post('id',0);
            $data['role_id']= $request->post('role_id',0);
            $data['account_name']= $request->post('account_name','');
            $data['account_email'] = $request->post('account_email','');
            $data['account_phone'] = $request->post('account_phone','');
            $data['account_status']= $request->post('account_status',0);

            $validator = Validator::make($data, $this->accountRequest->rules()['update'], $this->accountRequest->messages());
            if ($validator->fails()) {
                $result['msg'] = $validator->errors()->first();
                return response()->json($result);
            }

            if(($id < 1) || !($this->accountLogic->has((int)$id))){
                $result['msg'] = '请勿非法提交！';
                return response()->json($result);
            }

            if ($id==1 && $request->session()->get('admin_id') != 1){
                $result['msg'] = '不能修改超级管理员的信息！';
                return response()->json($result);
            }

            if ($id==1 && $data['role_id'] != 1){
                $result['msg'] = '不能修改超级管理员的角色！';
                return response()->json($result);
            }

            if ($this->accountLogic->update((int)$id, $data)){
                $result['status'] = true;
                $result['msg'] = '修改成功！';
                return response()->json($result);
            }

            return response()->json($result);

        } else {
            if ($id < 1) abort(404);
            $data = $this->accountLogic->find((int)$id);
            if (empty($data)) abort(404);
            $role = $this->roleLogic->all();
            return view('account.edit', ['role' => $role, 'data' => $data]);
        }
    }

    public function mod(Request $request, int $id = 0)
    {
        if ($request->isMethod('POST')) {
            $result = ['status'=>false, 'msg'=>'修改失败！'];
            $id= $request->post('id',0);
            $data['password'] = $request->post('password','');
            $data['password_confirm'] = $request->post('password_confirm','');

            if(($id < 1) || !($this->accountLogic->has((int)$id))){
                $result['msg'] = '请勿非法提交！';
                return response()->json($result);
            }

            if ($id==1 && $request->session()->get('admin_id') != 1){
                $result['msg'] = '不能修改超级管理员的密码！';
                return response()->json($result);
            }

            $validator = Validator::make($data, $this->accountRequest->rules()['modify'], $this->accountRequest->messages());
            if ($validator->fails()) {
                $result['msg'] = $validator->errors()->first();
                return response()->json($result);
            }

            $modelData['password'] = md5('fengadmin&' . $data['password']);
            if ($this->accountLogic->modify((int)$id, $modelData)){
                $result['status'] = true;
                $result['msg'] = '修改成功！';
                return response()->json($result);
            }

            return response()->json($result);
        } else {
            if ($id < 1) abort(404);
            $data = $this->accountLogic->find($id);
            if (empty($data)) abort(404);
            return view('account.mod', ['id'=>$id,'account'=>$data['account']]);
        }
    }

    public function status(Request $request, int $id = 0)
    {
        $result = ['status' => false, 'msg' => '状态设置失败！'];
        if ($id == 1){
            $result['msg'] = '不能改变超级管理员的状态！';
            return response()->json($result);
        }else{
            if ($request->session()->get("admin_id") == $id) {
                $result['msg'] = '不能改变当前账号的状态！';
                return response()->json($result);
            }
            if ($this->accountLogic->status((int)$id)){
                $result['status'] = true;
                $result['msg'] = '状态设置成功！';
                return response()->json($result);
            }
        }
        return response()->json($result);
    }

    public function delete(Request $request, $id = 0)
    {
        $result = ['status'=>false, 'msg'=>'删除失败！'];
        if(($id < 1) || !($this->accountLogic->has((int)$id))){
            $result['msg'] = '请勿非法提交！';
            return response()->json($result);
        }
        if ($id == 1){
            $result['msg'] = '超级管理员禁止删除！';
            return response()->json($result);
        }
        if ($id == $request->session()->get('admin_id')){
            $result['msg'] = '不允许删除当前登录账号！';
            return response()->json($result);
        }
        if ($this->accountLogic->delete((int)$id)){
            $result['status'] = true;
            $result['msg'] = '删除成功！';
            return response()->json($result);
        }
        return response()->json($result);
    }
}
